package com.huantai.common.util.time;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 起止年月相关的工具类
 * 
 * @author Ma Chao
 * @version 2018年6月12日 下午3:16:05
 */
public class YearMonthUtil {
    private YearMonthUtil() {}

    /** 每月开始第一天零时零分零秒 */
    private static final String FIRST_DAY_OF_MONTH = "-01 00:00:00";
    /** 开始时间 */
    private static final String BEGIN_TIME = "beginTime";
    /** 结束时间 */
    private static final String END_TIME = "endTime";

    /**
     * 获得当前月份的开始结束时间
     */
    public static Map<String, String> getThisMonthBeginEndTime() {
        Map<String, String> param = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        param.put(BEGIN_TIME, getFirstDayOfThisMonth(year, month));
        param.put(END_TIME, getFirstDayOfNextMonth(year, month));
        return param;
    }

    /**
     * 获得<code>year</code>年<code>month</code>月的开始结束时间
     */
    public static Map<String, String> getThisMonthBeginEndTime(int year, int month) {
        Map<String, String> param = new HashMap<>();
        param.put(BEGIN_TIME, getFirstDayOfThisMonth(year, month));
        param.put(END_TIME, getFirstDayOfNextMonth(year, month));
        return param;
    }

    /**
     * 获得从开始年月到结束年月之间的所有年月
     */
    public static List<String> getYearMonthFromBegin2End(String beginYearMonth,
            String endYearMonth) {
        List<String> yearMonthList = new ArrayList<>();
        int beginYear = getYear(beginYearMonth);
        int beginMonth = getMonth(beginYearMonth);
        int endYear = getYear(endYearMonth);
        int endMonth = getMonth(endYearMonth);

        int year = beginYear;
        while (year <= endYear) {
            int month = 1;
            if (year == beginYear) {
                month = beginMonth;
            }
            while (year == endYear ? month <= endMonth : month <= 12) {
                yearMonthList.add(getYearMonth(year, month));
                month++;
            }
            year++;
        }
        return yearMonthList;
    }

    /**
     * 拼接年月为YYYYMM格式
     */
    public static String getYearMonth(int year, int month) {
        return new StringBuilder().append(year).append(getFullMonth(month)).toString();
    }

    /**
     * 根据YYYYMM获得年份
     */
    public static int getYear(String yearMonth) {
        return Integer.parseInt(yearMonth.substring(0, 4));
    }

    /**
     * 根据YYYYMM获得月份
     */
    public static int getMonth(String yearMonth) {
        return Integer.parseInt(yearMonth.substring(4));
    }

    /**
     * 获取<code>year</code>年<code>month</code>月上个月01日零时零分零秒
     */
    public static String getFirstDayOfLastMonth(int year, int month) {
        // 如果是1月，则年份-1，月份=12
        if (month == 1) {
            year = year - 1;
            month = 12;
        }
        // 否则，年份不变，月份-1
        else {
            month = month - 1;
        }
        return getFirstDayOfThisMonth(year, month);
    }

    /**
     * 获取<code>year</code>年<code>month</code>月01日零时零分零秒
     */
    public static String getFirstDayOfThisMonth(int year, int month) {
        StringBuilder result = new StringBuilder();
        result.append(year).append("-").append(getFullMonth(month)).append(FIRST_DAY_OF_MONTH);
        return result.toString();
    }

    /**
     * 获取<code>year</code>年<code>month</code>月下个月01日零时零分零秒
     */
    public static String getFirstDayOfNextMonth(int year, int month) {
        // 如果是12月，则年份+1，月份=1
        if (month == 12) {
            year = year + 1;
            month = 1;
        }
        // 否则，年份不变，月份+1
        else {
            month = month + 1;
        }
        return getFirstDayOfThisMonth(year, month);
    }

    /**
     * 取MM格式的月份
     */
    private static String getFullMonth(int month) {
        if (month >= 10) {
            return month + "";
        } else {
            return "0" + month;
        }
    }
}
